package weaver.aiyh_pcn.workflow_to_customer.utils;

import java.util.List;


/**
 * @author EBU7-dev1-ayh
 * @create 2021/8/13 0013 15:58
 * sqlWhere 构建SQL的条件
 */


public class SQLWhere {
	private final StringBuilder sqlWhere;


	public SQLWhere(Where where) {
		this.sqlWhere = where.sqlWhere;
	}

	public StringBuilder getSqlWhere() {
		return sqlWhere;
	}


	public static Where creatWhere() {
		return new Where();
	}


	public static class Where {
		private final StringBuilder sqlWhere;
		private final WhereOperator whereOperator;

		public Where() {
			this.sqlWhere = new StringBuilder(" 1 = 1 ");
			this.whereOperator = new WhereOperator(this);
		}

		public WhereOperator and(String key) {
			this.sqlWhere.append(" and ");
			this.sqlWhere.append(key);
			return whereOperator;
		}

		public WhereOperator or(String key) {
			this.sqlWhere.append(" or ");
			this.sqlWhere.append(key);
			return whereOperator;
		}

		public SQLWhere create() {
			return new SQLWhere(this);
		}
	}


	public static class WhereOperator {
		private final Where where;

		public WhereOperator(Where where) {
			this.where = where;
		}

		public Where isNull(String key) {
			where.sqlWhere.append(" is null ");
			return where;
		}

		public Where isNotNUll(String key) {
			where.sqlWhere.append(" is not null ");
			where.sqlWhere.append(key);
			return where;
		}

		public Where equal(Object value) {
			where.sqlWhere.append(" = ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value);
			where.sqlWhere.append("'");
			return where;
		}

		public Where noEqual(Object value) {
			where.sqlWhere.append(" <> ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value);
			where.sqlWhere.append("'");
			return where;
		}

		public Where in(List<Object> list) {
			where.sqlWhere.append(" in ( ");
			for (int i = 0; i < list.size(); i++) {
				where.sqlWhere.append("'");
				where.sqlWhere.append(list.get(i));
				where.sqlWhere.append("'");
				if (i != list.size() - 1) {
					where.sqlWhere.append(" , ");
				}
			}
			where.sqlWhere.append(" ) ");
			return where;
		}

		public Where in(Object ...list) {
			where.sqlWhere.append(" in ( ");
			for (int i = 0; i < list.length; i++) {
				where.sqlWhere.append("'");
				where.sqlWhere.append(list[i]);
				where.sqlWhere.append("'");
				if (i != list.length - 1) {
					where.sqlWhere.append(" , ");
				}
			}
			where.sqlWhere.append(" ) ");
			return where;
		}

		public Where noIn(List<Object> list) {
			where.sqlWhere.append(" not in ( ");
			for (int i = 0; i < list.size(); i++) {
				where.sqlWhere.append("'");
				where.sqlWhere.append(list.get(i));
				where.sqlWhere.append("'");
				if (i != list.size() - 1) {
					where.sqlWhere.append(" , ");
				}
			}
			where.sqlWhere.append(" ) ");
			return where;
		}
		public Where noIn(Object ...list) {
			where.sqlWhere.append(" not in ( ");
			for (int i = 0; i < list.length; i++) {
				where.sqlWhere.append("'");
				where.sqlWhere.append(list[i]);
				where.sqlWhere.append("'");
				if (i != list.length - 1) {
					where.sqlWhere.append(" , ");
				}
			}
			where.sqlWhere.append(" ) ");
			return where;
		}

		public Where equalSql(Object value) {
			where.sqlWhere.append(" in ( ");
			where.sqlWhere.append(value);
			where.sqlWhere.append(" ) ");
			return where;
		}

		public Where noEqualSql(Object value) {
			where.sqlWhere.append(" not in ( ");
			where.sqlWhere.append(value);
			where.sqlWhere.append(" ) ");
			return where;
		}

		public Where like(Object value) {
			where.sqlWhere.append(" like ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value);
			where.sqlWhere.append("'");
			return where;
		}

		public Where greaterOrEqual(Object value) {
			where.sqlWhere.append(" >= ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value);
			where.sqlWhere.append("'");
			return where;
		}

		public Where lessOrEqual(Object value) {
			where.sqlWhere.append(" <= ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value);
			where.sqlWhere.append("'");
			return where;
		}

		public Where BetweenAnd(Object value1, Object value2) {
			where.sqlWhere.append(" between ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value1);
			where.sqlWhere.append("'");
			where.sqlWhere.append(" and ");
			where.sqlWhere.append("'");
			where.sqlWhere.append(value2);
			where.sqlWhere.append("'");
			return where;
		}

	}
}
